import { RectI, SpotI, StyleI } from '@/utils/CanvasUtils/CanvasType';
import CanvasUtils from '@/utils/CanvasUtils';
import Paint from '../Paint';
import Base from './BaseElement';

export interface SquareI extends StyleI {
	/** 矩形 */
	Rect: RectI;
}

export default class RectElement extends Base {

	public rect: SquareI;

	private save: RectI | null = null;

	public constructor(paint: Paint, rect: SquareI) {
		super(paint);
		this.rect = rect;
		this.save = null;
	}

	public Draw(canvas: CanvasUtils): void {
		const { Rect: { Spot: { X, Y }, Width, Height }, BackgroundColor, BorderColor, LineWidth } = this.rect;
		const save = this.save = { Spot: { X, Y }, Width, Height };
		canvas.Save();
		if (BackgroundColor) {
			canvas.FillStyle = BackgroundColor;
			canvas.FillRect(save);
		}
		if (BorderColor) {
			canvas.StrokeStyle = BorderColor;
			if (LineWidth) {
				canvas.LineWidth = LineWidth;
			}
			canvas.StrokeRect(save);
		}
		canvas.Restore();
	}

	public OnRange({ X: x, Y: y }: SpotI): boolean {
		if (!this.save) {
			return false;
		}
		const { Spot: { X, Y }, Width, Height } = this.save;
		return x >= X && x <= X + Width && y >= Y && y <= Y + Height;
	}
}
